package servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import pojo.ClientUsers;
import pojo.Merchant;
import service.ClientUsersDaoManager;
import service.MerchantManager;
import service.factory.ManagerFactory;


public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	private final static Logger logger = LogManager.getLogger(LoginServlet.class);
	private static ClientUsersDaoManager clientUsersDaoManager;

	public LoginServlet() {
		super();
		clientUsersDaoManager = ManagerFactory.getClientUserDaoManager();
	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String uname = request.getParameter("uname");
		String psd = request.getParameter("psd");
		ClientUsers user = clientUsersDaoManager.findByName(uname, psd);
		if(user == null)
		{
			request.getSession().setAttribute("error", "You username or account error! ");
			request.getSession().setAttribute("user", user);
			response.sendRedirect("login.html");
		}
		else
		{
			request.getSession().setAttribute("user", user);
			response.sendRedirect("showMerchants");
		}

	}
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doPost(req, resp);
	}
}
